package com.wego.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wego.service.base.BaseServiceImpl;
import com.wego.common.bean.PageBean;
import com.wego.common.utils.PageBeanUtil;
import com.wego.common.utils.SnowFlake;
import com.wego.entity.domain.Goods;
import com.wego.entity.domain.Order;
import com.wego.entity.domain.OrderItem;
import com.wego.entity.query.OrderQuery;
import com.wego.mapper.GoodsMapper;
import com.wego.mapper.OrderItemMapper;
import com.wego.mapper.OrderMapper;
import com.wego.service.OrderService;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单服务实现类
 *  @author hc
 */
@Service
public class OrderServiceImpl extends BaseServiceImpl<Order> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageBean<Order> selectPage(OrderQuery orderQuery) {
        if (orderQuery == null) {
            orderQuery = new OrderQuery();
        }
        //设置分页信息
        Page<Order> page = PageHelper.startPage(orderQuery.getPageNum(), orderQuery.getPageSize());
        //查询数据
        orderMapper.selectList(orderQuery);
        //将MyBatis提供的Page对象转换成我们自己的PageBean对象
        PageBean<Order> pageBean = PageBeanUtil.page2PageBean(page);
        return pageBean;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean genOrder(Long[] goodsId, Integer[] amount, Integer payMethodId, Long addressId, Long userId) {
        //订单项的主键
        Long[] orderItemIds = new Long[goodsId.length];

        //购买商品总价值
        BigDecimal totalMoney = new BigDecimal(0);
        for (int i = 0; i < goodsId.length; i++) {
            Goods goods = goodsMapper.selectByPrimaryKey(goodsId[i]);

            BigDecimal money = goods.getPrice2().multiply(new BigDecimal(amount[i]));
            totalMoney = totalMoney.add(money);

            OrderItem orderItem = OrderItem.builder()
                    .amount(amount[i])
                    .money(money)
                    .goodsId(goodsId[i])
                    .state(1)
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();
            //保存订单项
            orderItemMapper.insert(orderItem);
            //存放新添加订单项的编号
            orderItemIds[i] = orderItem.getId();
        }

        //雪花算法生成订单编号
        long orderId = SnowFlake.nextId();
        Order order = Order.builder()
                .id(orderId)
                .amount(goodsId.length)
                .money(totalMoney)
                .payMethodId(payMethodId)
                .userId(userId)
                .addressId(addressId)
                .state(1)
                .priority(50)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        //保存订单
        orderMapper.insert(order);

        //更新订单项，设置其order_id为新增加订单的id
        for (Long orderItemId : orderItemIds) {
            orderItemMapper.updateByPrimaryKeySelective(OrderItem.builder()
                    .id(orderItemId)
                    .orderId(orderId)
                    .build());
        }

        return true;
    }

}

